<?php
namespace Rest\Controller;

class ArticleController extends CommonController {
    /**
     * 添加文章到素材库
     *
     * @param     void
     * @return    void
     **/
    public function add() {
        $id = D('Article','Logic')->add($_POST);

        $this->success(array(
            'id'    =>  $id
        ));
    }
    /**
     * 判断文章是否存在
     *
     * @param     void
     * @return    void
     **/
    public function exists() {
        $title = I('post.title');
        verify($title,'notEmpty','标题不能为空');

        $cid = I('post.cid',0,'intval');
        verify($cid,'notEmpty','分类不能为空');

        $where = array(
            'title'     =>  $title,
            'cid.'.$cid =>  array('in',array(0,$cid)),
        );
        $ArticleMongoModel = D('Article','Mongo');
        $articleInfo = $ArticleMongoModel->where($where)->find();
        if($articleInfo) {
            $this->success($articleInfo);
        }
        // 查看文章是否存在，如果存在则将分类添加进入并返回结果
        $articleInfo = $ArticleMongoModel->where(array('title'=>$title))->find();
        if($articleInfo) {
            $articleInfo['cid'][$cid] = $cid;
            $ArticleMongoModel->save(array(
                'id'            =>  $articleInfo['id'],
                'cid'           =>  $articleInfo['cid'],
                'updatetime'    =>  NOW_TIME
            ));
            $this->success($articleInfo);
        }

        $this->error('文章《' . $title . '》不存在');

    }
}
